package com.excel.demo.excel.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExcelUtil
 * @Description TODO
 * @Author Administrator
 * @Date 2022/4/9 1:23
 * @Version 1.0
 */

public class ExcelUtil {
    /**
     * excel 导出
     *
     * @param list  要导出的数据
     * @param pojoClass  pojo类型
     * @param fileName  文件名称
     * @param exportParams  导出表格参数
     * @param response
     */
    public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName,
                                   ExportParams exportParams,
                                   HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel(fileName, response, workbook);
    }

    public static void exportExcel(List<Map<String, Object>> list, ExcelType type, String fileName,int count,
                                       HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(list,type);
        mergeMyStyle(workbook,count);
        downLoadExcel(fileName, response, workbook);
    }
    //只有合有效果，样式沒用
    private static void mergeMyStyle(Workbook workbook,int count){
        for (int i = 0; i < count; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            CellRangeAddress cellAddresses = new CellRangeAddress(2,2,6,7);
//            XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
//            style.setBorderBottom(BorderStyle.THIN); //下边框
//            style.setBorderLeft(BorderStyle.THIN);//左边框
//            style.setBorderTop(BorderStyle.THIN);//上边框
//            style.setBorderRight(BorderStyle.THIN);//右边框
//            style.setAlignment(HorizontalAlignment.CENTER); // 居中
//            XSSFFont font = (XSSFFont) workbook.createFont();
//
//            Row row = sheet.getRow(0);
//            Cell cell = row.getCell(0);
//
//            //设置字体大小;
//            font.setFontHeightInPoints((short)12 );
//            //设置字体名字;
//            font.setFontName("宋体");
//            //设置样式;
//            //在样式用应用设置的字体;
//            style.setFont(font);
//            //设置垂直对齐的样式为居中对齐;
//            style.setVerticalAlignment(VerticalAlignment.CENTER);
//            cell.setCellStyle(style);
//
            sheet.addMergedRegion(cellAddresses);
        }
    }

    /**
     * 下载文件
     *
     * @param fileName 文件名称
     * @param response
     * @param workbook excel数据
     */
    private static void downLoadExcel(String fileName, HttpServletResponse response,
                                      Workbook workbook)
            throws IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
